import Vue from "vue";
import App from "./App";
import "./uni.promisify.adaptor";

// import "./static/iconfont/iconfont.js";
// import "./static/iconfont/iconfont.css";
// import "./static/css/icon.css";

// import ElementUI from "element-ui";
// import "element-ui/lib/theme-chalk/index.css";
// Vue.use(ElementUI);

// #ifdef H5
// import Vconsole from "vconsole";
// const vConsole = new Vconsole();
// Vue.use(vConsole);

import jwx from "./wx";

Vue.prototype.$jwx = jwx;
// #endif

import navTitle from "./components/navTitle.vue";
Vue.component("navTitle", navTitle);

import Xskeleton from "./components/x-skeleton/x-skeleton.vue";
Vue.component("x-skeleton", Xskeleton);

import nodata from "./components/nodata.vue";
Vue.component("nodata", nodata);

import tabs from "./components/tabs.vue";
Vue.component("tabs", tabs);

import couponDescription from "./components/coupon-description.vue";
Vue.component("coupon-description", couponDescription);

import uView from "uview-ui";
Vue.use(uView);

import { VueJsonp } from "vue-jsonp";
Vue.prototype.$jsonp = VueJsonp;
Vue.use(VueJsonp);

Vue.config.productionTip = false;

App.mpType = "app";

const app = new Vue({
  ...App,
});
app.$mount();

Vue.prototype.bodyHidden = () => {
  document.body.style.overflow = "hidden"; // 阻止页面滚动
};

// #ifdef H5
import { getEnv } from "@/utils/env.js";
import {
  setGloBalData,
  setStorage,
  getStorage,
  getToken,
  removeStorage,
} from "@/utils/storage.js";
import baseUrl from "./servers/baseUrl";
import { oauthtoken, userinfo, updateParent } from "./servers/servers";
import { HTTP_STATUS, config } from "./servers/config";
if (getEnv() == "WX_PUBLIC") {
  let params = {};
  getQueryString();

  function getQueryString() {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    for (let i = 0; i < vars.length; i++) {
      let pair = vars[i].split("=");
      params[pair[0]] = pair[1];
    }
  }
  let appid = config.appId;
  if (params.code) {
    if (getToken()) {
      removeStorage("code");
      if (params.uid) {
        updateParent({
          id: params.uid,
        });
      }
    } else {
      setStorage("code", params.code);
      oauthtoken({
        code: params.code,
        grant_type: "wechat",
        appId: appid,
      }).then(async (res) => {
        console.log(res);
        let token = res.data.data.access_token;
        getApp().globalData.token = token;
        uni.setStorageSync("token", token);
        console.log(token);
        console.log(getApp().globalData.token);
        if (params.uid) {
          updateParent({
            id: params.uid,
          });
        }
        let userdata = await userinfo();
        if (userdata.data.code == HTTP_STATUS.RUSELT_SUCCESS) {
          let info = userdata.data.data;
          app.globalData.userinfo = info;
          setStorage("userinfo", info);
          setStorage("isPlace", info.isPlace);
          if (info.mobile) {
            app.globalData.is_authorization = true;
            uni.setStorageSync("is_authorization", true);
          } else {
            app.globalData.is_authorization = false;
            uni.setStorageSync("is_authorization", false);
          }
        }
      });
    }
  } else {
    // 线上测试：wxe474c525ff2a55ac
    if (!getToken()) {
      let state = params.uid ? params.uid : 1;
      window.location.href = `${baseUrl}/server-auth/oauth/wechat/userinfo?url=${window.location.href}&state=${state}`;
      // window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${window.location.href}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
    }
  }
} else if (getEnv() == "H5") {
  if (!getToken() && window.location.href.indexOf("diyPage") == -1) {
    console.log(window.location.href);
    if (window.location.href !== "pages/login/login") {
      setStorage("refUrl", window.location.href);
    }
    uni.reLaunch({
      url: "/pages/login/login",
    });
  }
}
// #endif

// #ifdef MP-WEIXIN
import { islogin } from "./utils/wxLogin";
islogin();
console.log("执行登录");
// #endif

// #ifdef H5
import routeListener from "./mixins/routeListener";

Vue.mixin(routeListener);
// #endif
